﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using HtmlAgilityPack;
using Doves.DataAccessLayer;
using Doves.BussinessLayer;


namespace Doves.Crawlement
{
    public class LinkCrawler
    {
        private HtmlDocument _doc;
        private Zomby _zomby;
        

		/// <summary>
		/// Creates an instance of a LinkCrawler.
		/// </summary>
		/// <param name="doc">The input HTML document. May not be null.</param>
        /// 
        public LinkCrawler(Zomby inZomby)
        {
            if (inZomby == null)
            {
                throw new ArgumentNullException("doc");
            }
            _zomby = inZomby;
            // Load document of this url
            try
            {
                // Load document of this url
                HtmlWeb hw = new HtmlWeb();
                _doc = hw.Load(_zomby.Url);
            }
            catch (System.Exception ex)
            {
                throw new ArgumentNullException("doc");
            }
        }

        public List<Link> ScanArticleLinks(LinkAnalizer inLinkAnalizer)
        {
            List<Link> CrawledLinkList = new List<Link>();
            Link aLink;
            if (inLinkAnalizer == null)
            {
                return null;
            }
            // Selected a article
            HtmlNodeCollection CrawledNodes = _doc.DocumentNode.SelectNodes(inLinkAnalizer.NodePattern);
            if (CrawledNodes == null)
            {
                return null;
            }
            // For each article to get earch part of it
            foreach (HtmlNode CrawledNode in CrawledNodes)
            {
                aLink = new Link();
                HtmlNode LinkNode = CrawledNode.SelectSingleNode(inLinkAnalizer.LinkPattern);
                HtmlNode ThumbNode = CrawledNode.SelectSingleNode(inLinkAnalizer.ThumbPattern);

                // Get link of this article
                if (LinkNode != null)
                {
                    aLink.Url = inLinkAnalizer.LinkCorrectedString + LinkNode.Attributes["href"].Value;
                }
                else
                {
                    // link not correct then stop
                    break;
                }

                // Get thumb of this article
                if (ThumbNode != null)
                {
                    aLink.ThumbImage = inLinkAnalizer.ThumbCorrectedString + ThumbNode.Attributes["src"].Value;
                }
                aLink.ZomId = _zomby.Id;
                aLink.CateId = _zomby.CateId;
                aLink.IsCrawled = false;
                aLink.CreatedTime = DateTime.Now;
                CrawledLinkList.Add(aLink);
            }
            return CrawledLinkList;
        }

        public List<Link> ScanArticleLinks()
		{
            List<Link> CrawledLinkList = new List<Link>();
            LinkAnalizerBO ObjBO = new LinkAnalizerBO();
            IEnumerable<LinkAnalizer> LinkAnalizerList = ObjBO.GetLinkAnalizerByZombyId(_zomby.Id);
            if (LinkAnalizerList == null)
            {
                return null;
            }
            foreach (LinkAnalizer analizer in LinkAnalizerList)
            {
                List<Link> TempList = new List<Link>();
                TempList = ScanArticleLinks(analizer);
                if (TempList != null)
                {
                    CrawledLinkList.AddRange(TempList);
                    TempList.Clear();
                }
            }
            return CrawledLinkList;
		}
    }
}
